package com.atguigu.ds.impl;

import com.atguigu.DynamicArray;
import com.atguigu.ds.Stack;

public class DynamicArrayStack<E> implements Stack<E> {

    private DynamicArray<E> arr = new DynamicArray<>();

    @Override
    public int size() {
        return arr.size();
    }

    @Override
    public boolean isEmpty() {
        return arr.isEmpty();
    }

    @Override
    public void push(E element) {
        arr.add(element);
    }

    @Override
    public E pop() {
        if(arr.isEmpty()){
            return null ;
        }
        return arr.remove(arr.size()-1);
    }

    @Override
    public E peek() {
        return arr.get(arr.size()-1);
    }

    @Override
    public String toString(){
        StringBuilder stringBuilder = new StringBuilder("[");
        for(int i = arr.size()-1 ; i>=0 ; i--){
            stringBuilder.append(arr.get(i)).append(i==0 ? "" : ",");
        }
        return stringBuilder.append("]").toString();
    }
}
